<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        /* If unlogin goto /index/login */
        $this->_helper->redirector('login', 'index');
    }

    public function loginAction()
    {
        $loginForm = new Application_Form_Login();
        $request = $this->getRequest();
        if ($request->isPost()) {
            if ($loginForm->isValid($request->getPost())) {
                $user = new Application_Model_User($request->getPost());

                try {
                    $auth = new Zend_Auth_Adapter_Digest();
                    $auth->setFilename(APPLICATION_PATH . '/configs/passwd')
                         ->setUsername($user->getUsername())
                         ->setRealm('EveryWriter')
                         ->setPassword($user->getPassword());
                    $auth_result = $auth->authenticate();

                    if ($auth_result->isValid()) { /* Authenticate successfully */
                        /*TODO: Add session code */
                        /*TODO: Add acl control code */

                        return $this->_helper->redirector('index', 'writer');
                    }

                } catch (Zend_Exception $e) {
                    echo $e->getMessage();
                }
            }
        }

        $this->view->loginForm = $loginForm;
    }

    public function logoutAction()
    {
        // action body
    }


}







